from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
import sys

name = 'OpenGL =)'
buttons =("Left", "Middle", "Right")
Tru = {True : 1, False : 0}

class Key:
   Up = "w"
   Down = "s"
   Left = "a"
   Right = "d"

def move(but, downup, x, y):
   if downup == 0:
      if but == 0: glRotated (90, 0, 0, 1)
      if but == 1: glRotated (90, 1, 0, 0)
      if but == 2: glRotated (90, 0, 1, 0)
      glutPostRedisplay()
   
   #print "Down/Up", downup
   #print "Button", buttons[but]
   #print "Mouse-xy:  (",x,",",y,")"
   #print "Window-xy: (",glutGet(GLUT_WINDOW_X),",",glutGet(GLUT_WINDOW_Y),")"

def shift(char, mx, my): 
   viewPort = glGetIntegerv(GL_VIEWPORT)
   glViewport(viewPort[0]+Tru[char==Key.Right]-Tru[char==Key.Left],
              viewPort[1]+Tru[char==Key.Up]-Tru[char==Key.Down],
              viewPort[2], viewPort[3])
   if (Tru[char==Key.Right]+Tru[char==Key.Left]+Tru[char==Key.Up]+Tru[char==Key.Down]) > 0: glutPostRedisplay()
   if Tru[char=="l"]:
      glLoadMatrixd(camera_matrix.get_inverse().to_opengl())
   #print glGetIntegerv(GL_MATRIX_MODE)
   mm = glGetIntegerv(GL_MODELVIEW_MATRIX)
   print "GL_MODELVIEW_MATRIX:"
   for i in range(0,len(mm)):
      print mm[i][0], mm[i][1], mm[i][2], mm[i][3]
   print ''
   print "GL_PROJECTION_MATRIX"
   pm = glGetIntegerv(GL_PROJECTION_MATRIX)
   for i in range(0,len(pm)):
      print pm[i][0], pm[i][1], pm[i][2], pm[i][3]
   print '--------------'

def main():
   glutInit(sys.argv)
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
   glutInitWindowSize(400,400)
   glutCreateWindow(name)

   glClearColor(0.,0.,0.,1.)
   glShadeModel(GL_SMOOTH)
   glEnable(GL_CULL_FACE)
   glEnable(GL_DEPTH_TEST)
   glEnable(GL_LIGHTING)
   lightZeroPosition = [10.,10.,2.,0.]
   lightZeroColor = [0.8,1.0,0.8,1.0] #green tinged
   glLightfv(GL_LIGHT0, GL_POSITION, lightZeroPosition)
   glLightfv(GL_LIGHT0, GL_DIFFUSE, lightZeroColor)
   glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.1)
   glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.05)
   glEnable(GL_LIGHT0)
   glutDisplayFunc(display)
   glMatrixMode(GL_PROJECTION)
   gluPerspective(80.,1.,1.,40.)
   glMatrixMode(GL_MODELVIEW)
   gluLookAt(0,0,5,
             0,0,0,
             0,1,0)
   glPushMatrix()
   glutMouseFunc(move)
   glutKeyboardFunc(shift)
   glutMainLoop()
   return

def display():
   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
   glPushMatrix()
   color = [1.0,0.,0.,1.]
   glMaterialfv(GL_FRONT,GL_DIFFUSE,color)
   #glutSolidSphere(0.4,40,40)
   glutWireTeapot(1.8)
   glPopMatrix()
   glutSwapBuffers()
   return

if __name__ == '__main__': main()